package chuangyi.com.org.DOMIHome.Utils;

import android.database.DataSetObserver;
import android.databinding.Bindable;
import android.databinding.BindingAdapter;
import android.databinding.DataBindingUtil;
import android.databinding.InverseBindingAdapter;
import android.databinding.ViewDataBinding;
import android.databinding.adapters.SpinnerBindingAdapter;
import android.graphics.Color;
import android.net.Uri;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.style.ForegroundColorSpan;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewParent;
import android.webkit.WebView;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Spinner;
import android.widget.SpinnerAdapter;
import android.widget.TextView;
import android.widget.Toast;

import com.nostra13.universalimageloader.core.DisplayImageOptions;

import java.io.File;
import java.math.BigDecimal;
import java.text.NumberFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Locale;

import chuangyi.com.org.DOMIHome.HomeActivity;
import chuangyi.com.org.DOMIHome.R;
import chuangyi.com.org.DOMIHome.databinding.FPickDateBinding;
import chuangyi.com.org.ViewModel.EditReward;
//import chuangyi.com.org.ViewModel.SpinnerModel;
//import fm.jiecao.jcvideoplayer_lib.JCVideoPlayerStandard;
//import io.vov.vitamio.widget.VideoView;

/**
 * Created by zxh on 16/11/17.
 */

public class ViewUtil {
    private static DisplayImageOptions options;

    static {
        DisplayImageOptions.Builder builder = new DisplayImageOptions.Builder();
        builder.showImageForEmptyUri(R.mipmap.logo);

        builder.cacheInMemory(false);
        builder.cacheOnDisk(true);
        options = builder.build();
    }

    @BindingAdapter("app:imageSrc")
    public static void imageLoader(ImageView imageView, String imageSrc) {
        if (imageSrc != null && !"null".equals(imageSrc)) {
            if (!imageSrc.startsWith("http") && !imageSrc.startsWith("file")) {
                File file = new File(imageSrc);
                if (file.exists()) {
                    imageSrc = "file://" + imageSrc;
                } else {
                    imageSrc = "";
                }
            }
            HomeActivity.getCurrent().imageLoader.displayImage(imageSrc, imageView, options);
        } else {
            HomeActivity.getCurrent().imageLoader.displayImage("", imageView, options);
        }
    }

    @BindingAdapter("app:localImageSrc")
    public static void imageLoader(ImageView view, int localImageSrc) {
        view.setImageResource(localImageSrc);
    }

    /**
     * 日期格式化为M月d日
     */
    @BindingAdapter("app:monthAndDay")
    public static void dateFormat(TextView textView, Date monthAndDay) {
        if (monthAndDay == null) {
            textView.setText("——");
        } else {
            textView.setText((new SimpleDateFormat("M月d日")).format(monthAndDay));
        }
    }

    /**
     * 日期格式化为yyyy/MM/dd
     */
    @BindingAdapter("app:date")
    public static void normalDate(TextView textView, Date monthAndDay) {
        if (monthAndDay == null) {
            textView.setText("——");
        } else {
            textView.setText((new SimpleDateFormat("yyyy/MM/dd")).format(monthAndDay));
        }
    }

    @BindingAdapter("app:fullDate")
    public static void setFullDate(TextView textView, Date monthAndDay) {
        if (monthAndDay == null) {
            textView.setText("——");
        } else {
            textView.setText((new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")).format(monthAndDay));
        }
    }

    @BindingAdapter("app:dateTime")
    public static void toString(TextView textView, Date date) {
        if (date == null) {
            textView.setText("--/--/-- --:--");
        } else {
            textView.setText(new SimpleDateFormat("yyyy/MM/dd HH:mm").format(date));
        }
    }

    @BindingAdapter("app:timeWithAm")
    public static void toTime(TextView textView, Date date) {
        if (date == null) {
            textView.setText("--/--/-- 上午--:--");
        } else {

        }
    }

//    /**
//     * 设置videoView的网络视频路径
//     */
//    @BindingAdapter("app:videoUri")
//    public static void setVideoUri(JCVideoPlayerStandard videoView, String url) {
//        try {
//            videoView.setUp(url, JCVideoPlayerStandard.SCREEN_LAYOUT_NORMAL, "");
////            if (url == null || "".equals(url)) {
////                videoView.setVideoPath("");
////                return;
////            } else {
////                Uri uri = Uri.parse(url);
////                videoView.setVideoURI(uri);
////            }
//        } catch (Exception ex) {
//        }
//    }

//    /**
//     * 设置videoView的网络视频路径
//     */
//    @BindingAdapter("app:videoCover")
//    public static void setVideoCover(JCVideoPlayerStandard videoView, String cover) {
//        try {
//            HomeActivity.getCurrent().imageLoader.displayImage(cover, videoView.thumbImageView, options);
//        } catch (Exception ex) {
//        }
//    }

//    @BindingAdapter("app:optionList")
//    public static void setOptions(final Spinner spinner, final SpinnerModel model) {
//        if (model != null) {
//            ArrayAdapter<String> adapter = new ArrayAdapter<String>(spinner.getContext(),
//                    android.R.layout.simple_spinner_item, model.getItems());
//            adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
//            spinner.setAdapter(adapter);
//            spinner.setOnItemSelectedListener(model.getListener());
//            spinner.setSelection(model.getSelectedItem());
//        }
//    }

    @BindingAdapter("app:setSelection")
    public static void setSelection(final Spinner spinner, final int pos) {
        spinner.setSelection(pos);
    }

    /**
     * double格式化为两位有效数字的字符串形式
     */
    @BindingAdapter("app:value")
    public static void setMoney(final TextView view, double value) {
        NumberFormat format = NumberFormat.getNumberInstance();
        format.setMaximumFractionDigits(2);
        view.setText(format.format(value));
    }

    @BindingAdapter("app:valueWithSymbol")
    public static void setValue(final TextView view, double value) {
        NumberFormat format = NumberFormat.getNumberInstance();
        format.setMaximumFractionDigits(2);
        String symbol = value < 0 ? "" : "+";
        view.setText(symbol + format.format(value));
    }

    @BindingAdapter("app:currency")
    public static void setCurrencyValue(final TextView view, double value) {
//        NumberFormat format = NumberFormat.getCurrencyInstance();
//        format.setMaximumFractionDigits(2);
        view.setText(String.format(Locale.CHINESE, "￥%.2f", value));
    }

    /**
     * TextView字符缩进
     *
     * @param view
     * @param text
     */
    @BindingAdapter("app:indentText")
    public static void setValue(final TextView view, String text) {
        SpannableStringBuilder span = new SpannableStringBuilder("缩进" + text);
        span.setSpan(new ForegroundColorSpan(Color.TRANSPARENT), 0, 2,
                Spanned.SPAN_INCLUSIVE_EXCLUSIVE);

        view.setText(span);
        ViewParent parent = view.getParent();
        if (parent instanceof LinearLayout) {
            LinearLayout owner = (LinearLayout) parent;
            int width = owner.getWidth();
            int heightSpec = View.MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2, View.MeasureSpec.AT_MOST);
            int widthSpec = View.MeasureSpec.makeMeasureSpec(width, View.MeasureSpec.EXACTLY);
            view.measure(widthSpec, heightSpec);
//            int height = view.getHeight();
//            ViewGroup.LayoutParams layoutParams = view.getLayoutParams();
//            layoutParams.height = height;
//            view.setLayoutParams(layoutParams);
        }
    }

    @BindingAdapter("app:url")
    public static void setUrl(final WebView view, String url) {
        view.loadUrl(url);
    }

    @BindingAdapter("app:leftTime")
    public static void setLeftTime(final TextView view, int leftTime) {
        if (leftTime < 0) {
            view.setText("悬赏尚未开始");
        } else if (leftTime == 0) {
            view.setText("悬赏已结束");
        } else {
            NumberFormat format = NumberFormat.getInstance();
            format.setMinimumIntegerDigits(2);
            int hour = leftTime / 3600;
            int minute = (leftTime - hour * 3600) / 60;
            int second = leftTime % 60;
            view.setText("距离结束还有" + format.format(hour) + "小时" + format.format(minute) + "分钟"
                    + format.format(second) + "秒");
        }
    }

    @BindingAdapter("app:leftHour")
    public static void setLeftHour(final TextView view, int leftTime) {
        if (leftTime < 0) {
            view.setText("悬赏尚未开始");
        } else if (leftTime == 0) {
            view.setText("悬赏已结束");
        } else {
            NumberFormat format = NumberFormat.getInstance();
            format.setMinimumIntegerDigits(2);
            int hour = leftTime / 3600;
            hour = hour <= 0 ? 1 : hour;
            view.setText("距离结束还有" + format.format(hour) + "小时");
        }
    }

    @BindingAdapter("app:nullDate")
    public static void setDate(final TextView view, Date date) {
        if (date == null) {
            view.setText("");
        } else {
            view.setText((new SimpleDateFormat("yyyy/MM/dd HH:mm")).format(date));
        }
    }

}
